package com.example.maili.woxiamg.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.maili.woxiamg.R;
import com.example.maili.woxiamg.base.BaseActivity;
import com.example.maili.woxiamg.base.ConstantClass;
import com.example.maili.woxiamg.utils.AppUtils;
import com.example.maili.woxiamg.utils.LogUtils;
import com.example.maili.woxiamg.utils.OkHttp3Manager;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import okhttp3.Request;

public class ForgetPasswordActivity extends BaseActivity {

    private TextView tv_getCode;
    private EditText et_phone;
    private EditText et_code;
    private EventHandler eventHandler;
    private String phone;
    private String code;
    private Button btn_next;

    @Override
    public void initView() {
        setContentView(R.layout.activity_forget_password);
        initCode();
        tv_getCode = (TextView) findViewById(R.id.tv_getCode);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        btn_next = (Button) findViewById(R.id.btn_next);
        tv_getCode.setOnClickListener(this);
        btn_next.setOnClickListener(this);
    }

    private void initCode() {
        // 如果希望在读取通信录的时候提示用户，可以添加下面的代码，并且必须在其他代码调用之前，否则不起作用；如果没这个需求，可以不加这行代码
        SMSSDK.setAskPermisionOnReadContact(true);
        // 创建EventHandler对象
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable) data;
                    String msg = throwable.getMessage();
                    AppUtils.toast(context, msg);
                } else {
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        LogUtils.wsf("aaaaaa:           " + data);
                        // 处理你自己的逻辑
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            boolean smart = (Boolean) data;
                            if (smart) {
                                //通过智能验证
                            } else {
                                //依然走短信验证
                            }
                        }
                    }
                }
            }
        };
        // 注册监听器
        SMSSDK.registerEventHandler(eventHandler);
    }

    @Override
    public void initdata() {

    }

    @Override
    protected void onClickListener(View view) {
        switch (view.getId()) {
            case R.id.tv_getCode:
                if (TextUtils.isEmpty(phone)) {
                    AppUtils.toast(context, "手机号不能为空");
                }
                SMSSDK.getVerificationCode("86", phone);
                break;
            case R.id.btn_next:
                submit();
                getData();
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ConstantClass.SUCCESS:
                    break;
                case ConstantClass.FAIL:
                    break;
            }
        }
    };

    private void getData() {
        OkHttp3Manager.postAsyn("https://webapi.sms.mob.com/sms/verify", new OkHttp3Manager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        LogUtils.wsf("res:            " + response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int status = jsonObject.getInt("status");
                            if (status == 200) {
                                AppUtils.toast(context, "验证成功");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new OkHttp3Manager.Param("appkey", "21bdb679635a0"),
                new OkHttp3Manager.Param("phone", phone),
                new OkHttp3Manager.Param("zone", "86"),
                new OkHttp3Manager.Param("code", code));
    }

    private void submit() {
        phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        code = et_code.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }
}
